package dontCare.gwtester.client.gwt.style;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

import dontCare.gwtester.client.tool.SourceUrl;
import dontCare.gwtester.client.tool.UnitWidget;

public class UiStyleTest extends UnitWidget{

	private static UiStyleTestUiBinder uiBinder = GWT
			.create(UiStyleTestUiBinder.class);

	interface UiStyleTestUiBinder extends UiBinder<Widget, UiStyleTest> {
	}

	interface BlueStyle extends CssResource {
		String bgBlue();
	}
	
	interface GreenStyle extends CssResource {
		String bgGreen();
	}
	
	public UiStyleTest() {
		initWidget(uiBinder.createAndBindUi(this));
		showStyles();
	}
	
	@UiField GreenStyle greenStyle;
	@UiField BlueStyle blueStyle;
	
	@UiField Button initStyle;
	@UiField Button addStyleRed;
	@UiField Button addStyleBlue;
	@UiField Button addStyleGreen;
	@UiField Button removeStyleRed;
	@UiField Button removeStyleBlue;
	@UiField Button removeStyleGreen;
	
	@UiField Label showCssClass;
	@UiField HTMLPanel styleTarget;
	@UiHandler("initStyle") void clickInitStyle(ClickEvent e){
		styleTarget.setStyleName("");
		showStyles();
	}
	@UiHandler("addStyleRed") void clickAddStyleRed(ClickEvent e){
		styleTarget.addStyleName("bgRed");
		showStyles();
	}
	@UiHandler("addStyleBlue") void clickAddStyleBlue(ClickEvent e){
		styleTarget.addStyleName(blueStyle.bgBlue());
		showStyles();
	}
	@UiHandler("addStyleGreen") void clickAddStyleGreen(ClickEvent e){
		styleTarget.addStyleName(greenStyle.bgGreen());
		showStyles();
	}
	@UiHandler("removeStyleRed") void clickRemoveStyleRed(ClickEvent e){
		styleTarget.removeStyleName("bgRed");
		showStyles();
	}
	@UiHandler("removeStyleBlue") void clickRemoveStyleBlue(ClickEvent e){
		styleTarget.removeStyleName(blueStyle.bgBlue());
		showStyles();
	}
	@UiHandler("removeStyleGreen") void clickRemoveStyleGreen(ClickEvent e){
		styleTarget.removeStyleName(greenStyle.bgGreen());
		showStyles();
	}
	
	private void showStyles(){
		showCssClass.setText("<div class='" + styleTarget.getStyleName() +"'></div>");	
	}
	@Override
	public List<String> getSourceUrl() {
		ArrayList<String> result = new ArrayList<String>();
		result.add(SourceUrl.genJava(UiStyleTest.class));
		result.add(SourceUrl.genUiXml(UiStyleTest.class));
		return result;
	}

	@Override
	public HTML getDescription() {
		return new HTML("使用<a href='http://google-web-toolkit.googlecode.com/svn/javadoc/2.4/com/google/gwt/resources/client/CssResource.html'>CssResource</a> 測試及跟外部 css 的優先權比較。");
	}
	@Override
	public String getName() {
		return "CssResource 測試";
	}

}
